

import java.awt.image.PixelGrabber;
import java.awt.Image;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import java.io.FileWriter;
import javax.swing.JFrame;
import java.awt.image.MemoryImageSource;
import java.awt.Graphics2D;
import java.io.File;

public class SpliceCount {
	int pixels[];
	int Aw, Ah, Bw, Bh;
	int Wmax, Hmax; // the size of splice image
	int arrayImageA[], arrayImageB[]; // arrays used to save the pixels
	String imagepathA = "images/try.gif"; // you can change the image path here
	String imagepathB = "images/fruit1.jpg";
	String outputimage = "SpliceCount.jpg";
	FileWriter fw; // used to write log
	int index = 0;
	ImageIcon imageA, imageB;

	public SpliceCount() {

		imageA = new ImageIcon(imagepathA);
		imageB = new ImageIcon(imagepathB);

		Aw = imageA.getIconWidth();
		Ah = imageA.getIconHeight();
		Bw = imageB.getIconWidth();
		Bh = imageB.getIconHeight();
		arrayImageA = handlepixels(imageA.getImage(), 0, 0, Aw, Ah);
		arrayImageB = handlepixels(imageB.getImage(), 0, 0, Bw, Bh);
		if (arrayImageA != null && arrayImageB != null) {

			doSplice(arrayImageA, arrayImageB);
			Image image = createImage(pixels);
			saveImage(image, outputimage);
		}
		if (fw != null) {
			try {
				fw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void doSplice(int[] imageA, int[] imageB) {
		pixels = imageB;
		Hmax = Bh;
		Wmax = Bw;
		int rangeFrom = Bw / 4;
		int rangeTo = Bw * 3 / 4;
		int range = Bw >> 1;

		for (int i = rangeFrom; i < Bw; i++) {
			float div = (float) (rangeTo - i) / (float) range;
			if (i > rangeTo) { // 渐变范围以外，不衰减
				div = 0;
			}
			float div1 = 1 - div; // 渐变左边范围以外，不衰减，用imgeA填充

			for (int j = 0; j < Bh; j++) {
				int pixel = pixels[j * Bw + i];
				int alpha = (pixel >> 24) & 0xff; // 分离imageB象素好相加
				int red = (pixel >> 16) & 0xff;
				int green = (pixel >> 8) & 0xff;
				int blue = (pixel) & 0xff;

				int alpha1, red1, green1, blue1;
				pixel = imageA[j * Aw + i]; // 准备分离imageA象素
				if (i > Aw || j > Ah) { // imageA 的大小不够，用白色填充
					alpha1 = 0xff;
					red1 = 0xff;
					green1 = 0xff;
					blue1 = 0xff;
				} else {
					alpha1 = (pixel >> 24) & 0xff;
					red1 = (pixel >> 16) & 0xff;
					green1 = (pixel >> 8) & 0xff;
					blue1 = (pixel) & 0xff;
				}

				alpha = 0xff;
				red = Math.round(red * div + red1 * div1); // 象素按每种颜色的衰减相加
				green = Math.round(green * div + green1 * div1);
				blue = Math.round(blue * div + blue1 * div1);

				pixels[j * Bw + i] = (alpha << 24) | (red << 16) | (green << 8) | (blue); // 合成颜色

			}
		}
	}

	public void writePixel2File(int color) {
		index++;

		if (fw == null) {
			try {
				fw = new FileWriter("pixel.txt");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			if (index % 100 == 0)
				fw.write("\n");

			fw.write("\t" + color);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Image createImage(int[] colors) {
		JFrame jf = new JFrame();
		Image modImg = jf.createImage(new MemoryImageSource(Wmax, Hmax, colors, 0, Wmax));
		return modImg;
	}

	public void saveImage(Image image, String filename) {
		java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(Wmax, Hmax, java.awt.image.BufferedImage.TYPE_INT_BGR);
		Graphics2D g2 = (Graphics2D) bi.getGraphics();

		g2.drawImage(image, 0, 0, null);

		try {
			javax.imageio.ImageIO.write(bi, "JPEG", new java.io.File(filename));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int[] handlepixels(Image img, int x, int y, int w, int h) {
		int[] pixel = new int[w * h];
		PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixel, 0, w);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			System.err.println("interrupted waiting for pixels!");

		}
		if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
			System.err.println("image fetch aborted or errored");
		}
		return pixel;
	}

	public static void main(String[] args) {
		SpliceCount splice = new SpliceCount();
	}
}